﻿/*****************************************************************
 * Copyright (C) Newegg Corporation. All rights reserved.
 * 
 * Author       :  Bill Li (Bill.S.Li@newegg.com)
 * Create Date  :  
 * Usage        :  
 *
 * RevisionHistory
 * Date         Author               Description
 * 
*****************************************************************/
using System;
using System.Text;
using Framework;

namespace TemplateEngine.TranslateCommand
{
    internal class ChooseTranslate : ITranslate
    {
        public string Translate(TemplateNode node, System.Xml.XmlNode xmlNode)
        {
            TemplateLogger.Debugger("开始解释Choose节点");

            // 校验内部节点
            int idx = 0;
            foreach (TemplateNode tNode in node.Nodes)
            {
                idx++;
                if (tNode.CommandStr != "when" && tNode.CommandStr != "other")
                {
                    TemplateEngineException ex = new TemplateEngineException("choose标签下只允许存在when或者other标签");
                    TemplateLogger.Error(ex);
                    throw ex;
                }
            }

            if (idx == 0)
            {
                TemplateEngineException ex = new TemplateEngineException("choose必须包含when或者other标签");
                TemplateLogger.Error(ex);
                throw ex;
            }

            if (node.Nodes[0].CommandStr == "other")
            {
                TemplateEngineException ex = new TemplateEngineException("choose标签下第一个标签不允许为other");
                TemplateLogger.Error(ex);
                throw ex;
            }

            // 开始执行
            bool flag = false;
            string str = string.Empty;
            foreach (var item in node.Nodes)
            {
                if (item.CommandStr == "when")
                {
                    str = item.TranslateNode(xmlNode);
                    flag = ((WhenTranslate)item.Translate).ExecuteFlag;
                    if (flag)
                    {
                        break;
                    }
                }
            }

            if (!flag)
            {
                // 调用other节点数据
                foreach (var item in node.Nodes)
                {
                    if (item.CommandStr == "other")
                    {
                        str = item.TranslateNode(xmlNode);
                        break;
                    }
                }
            }

            TemplateLogger.Debugger("结束解释Choose节点");
            return str;
        }
    }

    internal class WhenTranslate : ITranslate
    {
        private bool _flag = false;

        /// <summary>
        /// 执行判断是否成功
        /// </summary>
        internal bool ExecuteFlag
        {
            get
            {
                return _flag;
            }
        }

        public string Translate(TemplateNode node, System.Xml.XmlNode xmlNode)
        {
            var when = node.Attributes["test"];

            if (string.IsNullOrWhiteSpace(when))
            {
                TemplateEngineException ex = new TemplateEngineException("when标签下必须包含test属性");
                TemplateLogger.Error(ex);
                throw ex;
            }

            if (NodeTranslateUtil.ExecuteIf(when, xmlNode, node))
            {
                this._flag = true;
                StringBuilder html = new StringBuilder();
                html.Append(node.ResultStr);

                foreach (var item in node.Nodes)
                {
                    html = html.Replace("{[" + item.NodeGuid.ToString("N") + "]}", item.TranslateNode(xmlNode));
                }
                return html.ToString();
            }
            else
            {
                return string.Empty;
            }
        }
    }

    internal class OtherTranslate : ITranslate
    {
        public string Translate(TemplateNode node, System.Xml.XmlNode xmlNode)
        {
            string html = node.ResultStr;
            foreach (var item in node.Nodes)
            {
                html = html.Replace("{[" + item.NodeGuid.ToString("N") + "]}", item.TranslateNode(xmlNode));
            }

            return html.ToString();
        }
    }
}
